package com.wowudi.controller;

import com.wowudi.dto.Result;
import com.wowudi.entity.Dict;
import com.wowudi.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "字典管理")
@RequestMapping("api/system/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据id返回字典数据")
    @GetMapping("getDict/{dictId}")
    public Result getDict(@PathVariable("dictId") int dictId){
        List<Dict> dictList = dictService.findById(dictId);
        Result result = new Result();
        result.setData(dictList);
        return result;
    }


    @ApiOperation(value = "查询所有字典信息")
    @GetMapping("getDictAll")
    public Result getDictAll(String name){
        List<Dict> dictList = dictService.findDictAll(name);
        Result result = new Result();
        result.setData(dictList);
        return result;
    }


    @ApiOperation(value = "新增数据字典")
    @PostMapping("insertDict")
    public Result insertDict(@RequestBody Dict dict){
        int i = dictService.insertDict(dict);
        Result result = new Result();
        if (i == 1){
            result.setMessage("添加成功！");
        }else{
            result.setMessage("系统繁忙！");
        }
        return result;
    }


    @ApiOperation(value = "修改数据字典")
    @PostMapping("updateDict")
    public Result updateDict(@RequestBody Dict dict){
        int i = dictService.updateDict(dict);
        Result result = new Result();
        if (i == 1){
            result.setMessage("修改成功！");
        }else{
            result.setMessage("系统繁忙！");
        }
        return result;
    }


    @ApiOperation(value = "根据id删除数据字典")
    @PostMapping("deleteDictById")
    public Result deleteDictById(@RequestBody Dict dict){
        int i = dictService.deleteDict(dict);
        Result result = new Result();
        if (i == 1){
            result.setMessage("删除成功！");
        }else{
            result.setMessage("系统繁忙！");
        }
        return result;
    }

    //导入数据字典
    @PostMapping("importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        Result result = new Result();
        result.setMessage("成功");
        return result;
    }


    //导出数据字典接口
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

}
